{include file="public/header" /}

<div class="page">

    <div class="fixed-bar">
        <div class="item-title">
            <div class="subject">
                <h3>{$Think.lang.ds_promotion_xianshi}</h3>
                
                <h5></h5>
            </div>
            {include file="public/admin_items" /}
        </div>
    </div>



<table class="dssc-default-table">
    <tbody>
    <tr>
        <td class="w90 tr"><strong>{$Think.lang.xianshi_name}{$Think.lang.ds_colon}</strong></td>
        <td class="w120 tl">{$xianshi_info.xianshi_name}</td>
        <td class="w90 tr"><strong>{$Think.lang.start_time}{$Think.lang.ds_colon}</strong></td>
        <td class="w120 tl">{$xianshi_info.xianshi_starttime|date="Y-m-d H:i",###}</td>
        <td class="w90 tr"><strong>{$Think.lang.end_time}{$Think.lang.ds_colon}</strong></td>
        <td class="w120 tl">{$xianshi_info.xianshi_end_time|date="Y-m-d H:i",###}</td>
        <td class="w90 tr"><strong>折扣率{$Think.lang.ds_colon}</strong></td>
        <td class="w120 tl">{$xianshi_info.discount_rate}%</td>
        <td class="w90 tr"><strong>{$Think.lang.ds_state}{$Think.lang.ds_colon}</strong></td>
        <td class="w120 tl">{$xianshi_info.xianshi_state_text}</td>
    </tr>
</table>
<div class="alert">
    <strong>说明{$Think.lang.ds_colon}</strong>
    <ul>
        <li>{$Think.lang.xianshi_manage_goods_explain1}</li>
        <li>{$Think.lang.xianshi_manage_goods_explain2}</li>
    </ul>
</div>
{if $xianshi_info.editable}
        <a id="btn_show_goods_select" style="margin-left: 46px;"  class="dssc-btn dssc-btn-green" href="javascript:;"><i></i>添加商品</a>
{/if}
<!-- 商品搜索 -->
<div id="div_goods_select" class="div-goods-select" style="display: none;">
    <table class="search-form">
        <tr>
            <th class="w150" style="width: 137px;">
                <strong>第一步：商品名称</strong>
            </th>
            <td class="w160">
                <input id="search_goods_name" type="text w150" class="text" name="goods_name" value=""/>
            </td>
            <!-- <th class="w150" style="width: 137px;">
                <strong>型号</strong>
            </th>
            <td class="w160">
                <input id="" type="text w150" class="text" name="xinghao" value=""/>
            </td> -->
            <th class="w150" style="width: 137px;">
                <strong>品牌</strong>
            </th>
            <td class="w160">
                    <div id="ajax_brand" class="dssc-brand-select w180">
                        <div class="selection">
                            <input name="brand" id="b_name" value="" type="text"
                                   class="txt w180" readonly="readonly"/>
                            <input type="hidden" name="b_id" id="b_id" value="{$Request.param.b_id|default=''}"/>
                        </div>
                        <div class="dssc-brand-select-container">
                            <div class="brand-index" data-url="{:url('Common/ajax_get_brand')}">
                                <div class="letter" dstype="letter">
                                    <ul>
                                        <li><a href="javascript:void(0);" data-letter="all">全部品牌</a>
                                        </li>
                                        <li><a href="javascript:void(0);" data-letter="A">A</a></li>
                                        <li><a href="javascript:void(0);" data-letter="B">B</a></li>
                                        <li><a href="javascript:void(0);" data-letter="C">C</a></li>
                                        <li><a href="javascript:void(0);" data-letter="D">D</a></li>
                                        <li><a href="javascript:void(0);" data-letter="E">E</a></li>
                                        <li><a href="javascript:void(0);" data-letter="F">F</a></li>
                                        <li><a href="javascript:void(0);" data-letter="G">G</a></li>
                                        <li><a href="javascript:void(0);" data-letter="H">H</a></li>
                                        <li><a href="javascript:void(0);" data-letter="I">I</a></li>
                                        <li><a href="javascript:void(0);" data-letter="J">J</a></li>
                                        <li><a href="javascript:void(0);" data-letter="K">K</a></li>
                                        <li><a href="javascript:void(0);" data-letter="L">L</a></li>
                                        <li><a href="javascript:void(0);" data-letter="M">M</a></li>
                                        <li><a href="javascript:void(0);" data-letter="N">N</a></li>
                                        <li><a href="javascript:void(0);" data-letter="O">O</a></li>
                                        <li><a href="javascript:void(0);" data-letter="P">P</a></li>
                                        <li><a href="javascript:void(0);" data-letter="Q">Q</a></li>
                                        <li><a href="javascript:void(0);" data-letter="R">R</a></li>
                                        <li><a href="javascript:void(0);" data-letter="S">S</a></li>
                                        <li><a href="javascript:void(0);" data-letter="T">T</a></li>
                                        <li><a href="javascript:void(0);" data-letter="U">U</a></li>
                                        <li><a href="javascript:void(0);" data-letter="V">V</a></li>
                                        <li><a href="javascript:void(0);" data-letter="W">W</a></li>
                                        <li><a href="javascript:void(0);" data-letter="X">X</a></li>
                                        <li><a href="javascript:void(0);" data-letter="Y">Y</a></li>
                                        <li><a href="javascript:void(0);" data-letter="Z">Z</a></li>
                                        <li><a href="javascript:void(0);" data-letter="0-9">其他</a>
                                        </li>
                                    </ul>
                                </div>
                                <div class="search" dstype="search"><input name="search_brand_keyword"
                                                                           id="search_brand_keyword" type="text"
                                                                           class="text"/><a href="javascript:void(0);"
                                                                                            class="dssc-btn-mini"
                                                                                            style="vertical-align: top;">Go</a>
                                </div>
                            </div>
                            <div class="brand-list" dstype="brandList">
                                <ul dstype="brand_list">
                                    {notempty name="brand_list"}
                                    {foreach name="brand_list" item="val"}
                                    <li data-id='{$val.brand_id}' data-name='{$val.brand_name}'>
                                        <em>{$val.brand_initial}</em>{$val.brand_name}
                                    </li>
                                    {/foreach}
                                    {/notempty}
                                </ul>
                            </div>
                            <div class="no-result" dstype="noBrandList" style="display: none;">
                                {$Think.lang.no_brand_result}
                            </div>
                        </div>
                    </div>
            </td>
            
            <td class="w70 tc">
                <a href="javascript:void(0);" id="btn_search_goods" class="dssc-btn"/><i class="iconfont">&#xe718;</i>{$Think.lang.ds_search}</a>
            </td>
            <td class="w10"></td>
            <td>
                <p class="hint">不输入名称直接搜索将显示所有普通商品，特殊商品不能参加。</p>
            </td>
        </tr>
    </table>
    <div id="div_goods_search_result" class="search-result"></div>
    <a id="btn_hide_goods_select" class="close" href="javascript:void(0);">X</a> </div>



<table class="dssc-default-table">
    <thead>
    <tr>
        <th class="w150">图片</th>
        <th class="tl">活动名称</th>
        <th class="tl">{$Think.lang.goods_name}</th>
        <th class="tl">供应商名称</th>
        <th class="tl">供应商价格</th>
        <th class="tl">销售运营价格</th>
        <th class="tl">折扣后价格</th>
        <th class="w200">{$Think.lang.ds_handle}</th>
    </tr>
    </thead>
    <tbody id="xianshi_goods_list">
    <tr id="xianshi_goods_list_norecord" style="display:none">
        <td class="norecord" colspan="20"><div class="warning-option"><i class="iconfont">&#xe64c;</i><span>{$Think.lang.no_record}</span></div></td>
    </tr>
    </tbody>
</table>

<div id="dialog_edit_xianshi_goods" class="eject_con" style="display:none;">
    <input id="dialog_xianshigoods_id" type="hidden">
    <dl><dt>{$Think.lang.goods_store_price}：</dt><dd><span id="dialog_edit_goods_price"></span></dd></dl>
    <dl><dt>{$Think.lang.discount_price}：</dt><dd><input id="dialog_edit_xianshi_price" type="text" class="text w70"><em class="add-on"><i class="iconfont">&#xe65c;</i></em>
        <p id="dialog_edit_xianshi_goods_error" style="display:none;"><label for="dialog_edit_xianshi_goods_error" class="error"><i class='iconfont'>&#xe64c;</i>{$Think.lang.discount_price_cannot_empty}</label></p>
    </dl>
    <div class="eject_con">
        <div class="bottom pt10 pb10"><a id="btn_edit_xianshi_goods_submit" class="submit" href="javascript:void(0);">{$Think.lang.ds_submit}</a></div>
    </div>
</div>
</div>
<script id="xianshi_goods_list_template" type="text/html">
    <tr class="bd-line">
        <td><div style="width: 144px;" class="pic-thumb"><a ><img src="http://33wang.douyinhot.com/<%=goods_image%>" alt=""></a></div></td>
        <td class="tl"><dl class="goods-name"><dt><%=xianshi_name%></dt></dl></td>
        <td class="tl"><dl class="goods-name"><dt><%=goods_name%></dt></dl></td>
        <td class="tl"><dl class="store_name"><dt><%=store_name%></dt></dl></td>

        <td class="tl"><dl class="goods-name"><dt><%=goods_price%></dt></dl></td>
        <td class="tl"><dl class="goods-name"><dt><%=sale_price%></dt></dl></td>
        <td class="tl"><dl class="store_name"><dt><%=discount_price%></dt></dl></td>
        <td class="dscs-table-handle">
            {if $xianshi_info.editable}
            <a style="margin-left: 77px;" dstype="btn_del_xianshi_goods"  data-xianshi-goods-id="<%=xianshigoods_id%>" class="dsui-btn-del"><i class="iconfont"></i>{$Think.lang.ds_del}</a>
            {/if}
        </td>
    </tr>
</script>
<script type="text/javascript" src="{$Think.ADMIN_SITE_ROOT}/js/jquery.edit.js" charset="utf-8"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/jquery.mousewheel.js"></script>
<script type="text/javascript" src="{$Think.PLUGINS_SITE_ROOT}/mlselection.js" charset="utf-8"></script>
<script src="{$Think.PLUGINS_SITE_ROOT}/template.min.js"></script>
<script type="text/javascript">
/* AJAX选择品牌 */
$("#ajax_brand").brandinit();
    $(document).ready(function () {

        // 当前编辑对象，默认为空
        $edit_item = {};

        //现实商品搜索
        $('#btn_show_goods_select').on('click', function () {
            $('#div_goods_select').show();
        });

        //隐藏商品搜索
        $('#btn_hide_goods_select').on('click', function () {
            $('#div_goods_select').hide();
        });

        //搜索商品
        $('#btn_search_goods').on('click', function () {
            var url = "{:url('Promotionxianshi/goods_select')}";
            var datas = $.param({goods_name: $('#search_goods_name').val()});
            $('#div_goods_search_result').load(url, datas);
        });
        $('#div_goods_search_result').on('click', '.pagination li a', function () {
            $('#div_goods_search_result').load($(this).attr('href'));
            return false;
        });

        //添加限时折扣商品弹出窗口
        $('#div_goods_search_result').on('click', '[dstype="btn_add_xianshi_goods"]', function () {

            $('#dialog_goods_id').val($(this).attr('data-goods-id'));
            $('#dialog_goods_name').text($(this).attr('data-goods-name'));
            $('#dialog_goods_price').text($(this).attr('data-goods-price'));
            $('#dialog_input_goods_price').val($(this).attr('data-goods-price'));
            $('#dialog_goods_img').attr('src', $(this).attr('data-goods-img'));
            $('#dialog_add_xianshi_goods').ds_show_dialog({width: 450, title: '{$Think.lang.promotion_add_goods}'});
            $('#dialog_xianshi_price').val('');
            $('#dialog_add_xianshi_goods_error').hide();

        });

        //添加限时折扣商品
        $('#div_goods_search_result').on('click', '#btn_submit', function () {
            var goods_id = $('#dialog_goods_id').val();
            var xianshi_id = '{$Request.param.xianshi_id}';
            var goods_price = Number($('#dialog_input_goods_price').val());
            // var xianshi_price = Number($('#dialog_xianshi_price').val());
            // if (!isNaN(xianshi_price) && xianshi_price > 0 && xianshi_price < goods_price) {
            if (goods_price) {
                $.post("{:url('Promotionxianshi/xianshi_goods_add')}",
                    // {goods_id: goods_id, xianshi_id: xianshi_id, xianshi_price: xianshi_price},
                    {goods_id: goods_id, xianshi_id: xianshi_id},
                    function (data) {
                        if (data.result) {
                            $('#dialog_add_xianshi_goods').hide();
                            $('#xianshi_goods_list').prepend(template.render('xianshi_goods_list_template', data.xianshi_goods)).hide().fadeIn('slow');
                            $('#xianshi_goods_list_norecord').hide();
                            layer.msg(data.message);
                        } else {
                            layer.msg(data.message);
                        }
                    },
                    'json');
            } else {
                $('#dialog_add_xianshi_goods_error').show();
            }
        });

        //编辑限时活动商品
        $('#xianshi_goods_list').on('click', '[dstype="btn_edit_xianshi_goods"]', function () {
            $edit_item = $(this).parents('tr.bd-line');
            var xianshigoods_id = $(this).attr('data-xianshi-goods-id');
            var xianshi_price = $edit_item.find('[dstype="xianshi_price"]').text();
            var goods_price = $(this).attr('data-goods-price');
            $('#dialog_xianshigoods_id').val(xianshigoods_id);
            $('#dialog_edit_goods_price').text(goods_price);
            $('#dialog_edit_xianshi_price').val(xianshi_price);
            $('#dialog_edit_xianshi_goods').ds_show_dialog({width: 450, title: '{$Think.lang.modify_price}'});
        });

        $('#btn_edit_xianshi_goods_submit').on('click', function () {
            var xianshigoods_id = $('#dialog_xianshigoods_id').val();
            var xianshi_price = Number($('#dialog_edit_xianshi_price').val());
            var goods_price = Number($('#dialog_edit_goods_price').text());
            if (!isNaN(xianshi_price) && xianshi_price > 0 && xianshi_price < goods_price) {
                $.post("{:url('Promotionxianshi/xianshi_goods_price_edit')}",
                    {xianshigoods_id: xianshigoods_id, xianshi_price: xianshi_price},
                    function (data) {
                        if (data.result) {
                            $edit_item.find('[dstype="xianshi_price"]').text(data.xianshi_price);
                            $edit_item.find('[dstype="xianshi_discount"]').text(data.xianshi_discount);
                            $('#dialog_edit_xianshi_goods').hide();
                        } else {
                            layer.msg(data.message);
                        }
                    }, 'json'
                );
            } else {
                $('#dialog_edit_xianshi_goods_error').show();
            }
        });

        //删除限时活动商品
        $('#xianshi_goods_list').on('click', '[dstype="btn_del_xianshi_goods"]', function () {
            var $this = $(this);
            layer.confirm('{$Think.lang.ds_ensure_del}', {
                btn: ['{$Think.lang.ds_ok}', '{$Think.lang.ds_cancel}'],
                title: false,
            }, function () {
                var xianshigoods_id = $this.attr('data-xianshi-goods-id');

                $.post("{:url('Promotionxianshi/xianshi_goods_delete')}",{xianshigoods_id: xianshigoods_id},function (data) {
                    if (data.result) {
                        $this.parents('tr').hide('slow', function () {
                            var xianshi_goods_count = $('#xianshi_goods_list').find('.bd-line:visible').length;
                            if (xianshi_goods_count <= 0) {
                                $('#xianshi_goods_list_norecord').show();
                            }
                        });
                        layer.msg('删除成功');
                    } else {
                        layer.msg(data.message);
                    }
                }, 'json');
            });
        });

        //渲染限时折扣商品列表
        xianshi_goods_array = $.parseJSON('{:json_encode($xianshi_goods_list)}');
        if (xianshi_goods_array.length > 0) {
            var xianshi_goods_list = '';
            $.each(xianshi_goods_array, function (index, xianshi_goods) {
                xianshi_goods_list += template.render('xianshi_goods_list_template', xianshi_goods);
            });
            $('#xianshi_goods_list').prepend(xianshi_goods_list);
        } else {
            $('#xianshi_goods_list_norecord').show();
        }
    });
</script>

